package org.khd.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.khd.base.annotation.BasePermission;
import org.khd.base.utils.AjaxResult;
import org.khd.base.utils.PageList;
import org.khd.org.domain.Department;
import org.khd.org.query.DepartmentQuery;
import org.khd.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dep")
@Api(value = "部门的API",description = "部门相关的CRUD功能")
@BasePermission(name = "部门管理")
public class DepartmentController {

    @Autowired
    private IDepartmentService iDepartmentService;

    //新增和修改Department数据
//    @RequestMapping("/insertOrUpdateDep")
    @PutMapping
    @BasePermission(name = "部门的添加或修改")
    public AjaxResult insertOrUpdateDep(@RequestBody Department department){
        try {
            if (department.getId()==null){
                iDepartmentService.insertDep(department);
            }else {
                iDepartmentService.updateDep(department);
            }
            return AjaxResult.ARDefault();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ARDefault().setSuccess(false).setMsg("操作失败" + e.getMessage());
        }
    }

    //通过id删除Department数据
//    @RequestMapping("/deleteDepById")
    @DeleteMapping("/{id}")
    @BasePermission(name = "删除单个部门")
    public AjaxResult deleteDepById(@PathVariable("id") Long id){
        try {
            iDepartmentService.deleteDepById(id);
            return AjaxResult.ARDefault();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ARDefault().setSuccess(false).setMsg("操作失败" + e.getMessage());
        }
    }

    //批量删除Department数据
    @PatchMapping
    @BasePermission(name = "批量删除部门")
    public AjaxResult patchDeleteDep(@RequestBody List<Long> ids){
        try {
            iDepartmentService.patchDeleteDep(ids);
            return AjaxResult.ARDefault();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ARDefault().setSuccess(false).setMsg("操作失败" + e.getMessage());
        }
    }

    //通过id查询Department数据
//    @RequestMapping("/queryDepById")
    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询部门")
    @BasePermission(name = "查询单个部门")
    public Department queryDepById(@PathVariable("id") @ApiParam(value = "部门id") Long id){
        try {
            return iDepartmentService.queryDepById(id);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    //查询Department数据
//    @RequestMapping("/queryDepAll")
    @GetMapping
    @BasePermission(name = "查询所有部门")
    public List<Department> queryDepAll(){
        try {
            return iDepartmentService.queryDepAll();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    //查询Department每页数据
    @PostMapping
    @BasePermission(name = "部门分页查询")
    public PageList<Department> queryDepPage(@RequestBody DepartmentQuery query){
        try {
            return iDepartmentService.queryDepPage(query);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    //加载部门树
    @GetMapping("/tree")
    @BasePermission(name = "部门树")
    public List<Department> queryDepTree(){
        try {
            return iDepartmentService.queryDepTree();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

}
